#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from controller.repairInfoController import RepairInfoController
from utils import commons
from utils.response_code import RET


class RepairInfoResource(Resource):

    # get
    @classmethod
    def get(cls, repairID=None):
        if repairID:
            kwargs = {
                'repairID': repairID
            }

            res = RepairInfoController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('repairPlace', location='args', required=False, help='repairPlace参数类型不正确或缺失')
        parser.add_argument('repairType', location='args', required=False, help='repairType参数类型不正确或缺失')
        parser.add_argument('repairDetail', location='args', required=False, help='repairDetail参数类型不正确或缺失')
        parser.add_argument('contactName', location='args', required=False, help='contactName参数类型不正确或缺失')
        parser.add_argument('contactPhone', location='args', required=False, help='contactPhone参数类型不正确或缺失')
        parser.add_argument('status', location='args', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('userID', location='args', required=False, help='userID参数类型不正确或缺失')
        parser.add_argument('workID', location='args', required=False, help='workID参数类型不正确或缺失')
        parser.add_argument('createTime', location='args', required=False, help='createTime参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = RepairInfoController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    # delete
    @classmethod
    def delete(cls, repairID=None):
        if repairID:
            kwargs = {
                'repairID': repairID
            }

        else:
            parser = reqparse.RequestParser()
            parser.add_argument('repairPlace', location='form', required=False, help='repairPlace参数类型不正确或缺失')
            parser.add_argument('repairType', location='form', required=False, help='repairType参数类型不正确或缺失')
            parser.add_argument('repairDetail', location='form', required=False, help='repairDetail参数类型不正确或缺失')
            parser.add_argument('contactName', location='form', required=False, help='contactName参数类型不正确或缺失')
            parser.add_argument('contactPhone', location='form', required=False, help='contactPhone参数类型不正确或缺失')
            parser.add_argument('status', location='form', required=False, help='status参数类型不正确或缺失')
            parser.add_argument('userID', location='form', required=False, help='userID参数类型不正确或缺失')
            parser.add_argument('workID', location='form', required=False, help='workID参数类型不正确或缺失')
            parser.add_argument('createTime', location='form', required=False, help='createTime参数类型不正确或缺失')
            
            # Pass in the ID list for multiple deletions
            parser.add_argument('repairID', type=str, location='form', required=False, help='repairID参数类型不正确或缺失')

            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

        res = RepairInfoController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put
    @classmethod
    def put(cls, repairID):
        if not repairID:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('repairPlace', location='form', required=False, help='repairPlace参数类型不正确或缺失')
        parser.add_argument('repairType', location='form', required=False, help='repairType参数类型不正确或缺失')
        parser.add_argument('repairDetail', location='form', required=False, help='repairDetail参数类型不正确或缺失')
        parser.add_argument('contactName', location='form', required=False, help='contactName参数类型不正确或缺失')
        parser.add_argument('contactPhone', location='form', required=False, help='contactPhone参数类型不正确或缺失')
        parser.add_argument('status', location='form', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('userID', location='form', required=False, help='userID参数类型不正确或缺失')
        parser.add_argument('workID', location='form', required=False, help='workID参数类型不正确或缺失')
        parser.add_argument('createTime', location='form', required=False, help='createTime参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['repairID'] = repairID

        res = RepairInfoController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # add
    @classmethod
    def post(cls):
        '''
        RepairInfoList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('RepairInfoList', type=str, location='form', required=False, help='RepairInfoList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('RepairInfoList'):
            res = RepairInfoController.add_list(**kwargs)

        else:
            parser.add_argument('repairPlace', location='form', required=False, help='repairPlace参数类型不正确或缺失')
            parser.add_argument('repairType', location='form', required=False, help='repairType参数类型不正确或缺失')
            parser.add_argument('repairDetail', location='form', required=False, help='repairDetail参数类型不正确或缺失')
            parser.add_argument('contactName', location='form', required=False, help='contactName参数类型不正确或缺失')
            parser.add_argument('contactPhone', location='form', required=False, help='contactPhone参数类型不正确或缺失')
            parser.add_argument('status', location='form', required=False, help='status参数类型不正确或缺失')
            parser.add_argument('userID', location='form', required=False, help='userID参数类型不正确或缺失')
            parser.add_argument('workID', location='form', required=False, help='workID参数类型不正确或缺失')
            parser.add_argument('createTime', location='form', required=False, help='createTime参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = RepairInfoController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
